//
//  OAuthViewController.swift
//  新浪微博(day01练习)
//
//  Created by KeY on 16/1/13.
//  Copyright © 2016年 KeY. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class OAuthViewController: UIViewController {

    let webView = UIWebView()
    
    @objc private func  close() {
    
        dismissViewControllerAnimated(true, completion: nil)
    
    }
    //设置自动填充账号密码内容
    @objc private func defaultAccount() {
    
        //根据网络监视得到账号和密码的value修改其值
//        let jsString = "document.getElementById('userId').value = 'key19910101@sina.com', document.getElementById('passwd').value = 'a010104781'"
        let jsString = "document.getElementById('userId').value = 'leiggee@126.com', document.getElementById('passwd').value = 'oyonomg' "
webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBar()
        loadOAuthPage()
        
        // Do any additional setup after loading the view.
    }
    
    //设置webView的BarButtonIrem
    private func setNavBar() {
    
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充测试账号", style: .Plain, target: self, action: "defaultAccount")
    }
    
 
    //拼接授权页面信息
    private func loadOAuthPage() {
        //获取URl
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        //可选URL
        let url = NSURL(string: urlString)!
        //获取request
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
    }
}

extension OAuthViewController: UIWebViewDelegate {
    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //如果获取不到url 就不加载网页
        guard let urlString = request.URL?.absoluteString else {
        //当获取不到URL字符串时就不加载网页
        return false
        }
        
        if urlString.containsString("code=") {
        
            //如果获取不到这个字段,就返回false 不加载
            guard let query = request.URL?.query else {
            
                return false
            }
            //
            let codeStr = "code="
            
            let code = (query as NSString).substringFromIndex(codeStr.characters.count)
            loadAccessToken(code)
            
            //不让他跳转到回调界面
            return false
        }
        return true
    }
    private func loadAccessToken(code: String) {
    
        //用户信息
        UserAccountViewModel().loadAccessToken(code)
    }
    
}

